// src/stores/aclStore.js
import { defineStore } from 'pinia';
import { AclApi } from "@/api/acl/index.js";
import { ElMessage } from "element-plus";

export const useAclStore = defineStore('acl', {
    state: () => ({
        aclList: [],
        pageInfo: {
            pageSize: 10,
            pageNum: 1,
            total: 0,
            totalPage: 0
        }
    }),
    actions: {
        async getAclList() {
            const res = await AclApi.getAclList({
                page: this.pageInfo.pageNum,
                size: this.pageInfo.pageSize
            });
            this.aclList = res.data.records;
            this.pageInfo.pageSize = res.data.pageSize;
            this.pageInfo.pageNum = res.data.pageNumber;
            this.pageInfo.total = res.data.totalRow;
            this.pageInfo.totalPage = res.data.totalPage;
        },

        // 添加权限
        async addAcl(acl) {
            const res = await AclApi.addAcl(acl);
            if (res.code == 200) {
                ElMessage.success('添加权限成功');
                this.getAclList();
            } else {
                ElMessage.error('添加权限失败');
            }
        },

        // 更新权限信息
        async updateAcl(acl) {
            console.log("------------------------")
            console.log("acl",acl)
            const res = await AclApi.updateAcl(acl);
            console.log("res", res)
            if (res.code == 200) {
                ElMessage.success('更新权限信息成功');
                this.getAclList();
            } else {
                ElMessage.error('更新权限信息失败');
            }
        },

        // 删除权限
        async deleteAcl(id) {
            const res = await AclApi.deleteAcl(id);
            if (res.code == 200) {
                ElMessage.success('删除权限成功');
                this.getAclList();
            } else {
                ElMessage.error('删除权限失败');
            }
        },

        // 根据ID获取权限信息
        async getAclById(id) {
            const res = await AclApi.getAclById(id);
            if (res.code == 200) {
                this.currentAcl = res.data;
                return res.data;
            } else {
                ElMessage.error('获取权限信息失败');
                return null;
            }
        }
    }
});
